#IMPORTING ALL THE IMPORTANT MODULE FILES
import numpy as np
import os
from sys import argv
import pickle
import random


#STARTING THE SCRIPT.
#THE MOVIE LENS DATASET IS BEING USED CURRENTLY
#THE FUNCTION MIGHT HAVE TO BE CHANGED FOR OTHER DATA
script , ratings_file = argv;

#PRINT OUT THE SCRIPT NAME AND THE RATINGS FILE NAME
#JUST TO MAKE SURE EVERYTHING GOT READ PROPERLY
print "Script name is %s" % script
print "Ratings file name is %s" % ratings_file

##OPEN THE INPUT FILE
input_file = open(ratings_file , "r")

users_list = []
movies_list = []

#START READING THE INPUT FILE LINE BY LINE
for line in input_file :
        #split the line
        parts = line.split('::')
        users_list.append(parts[0])
        movies_list.append(parts[1])
input_file.close()


#MAKE THE USERS AND MOVIE LISTS INTO A SET TO GET RID OF DUPLICATES
users_list = set(users_list)
movies_list = set(movies_list)

#NOW CONVERT THE SET BACK INTO A LIST FOR EASY ITERATION
# ****INEFFICIENT!!!! PLEASE CHANGE!!!!
users_list = list(users_list)
movies_list = list(movies_list)

#NOW CREATE THE RATINGS MATRIX
m = len(users_list)
n = len(movies_list)
RatingsMatrix = np.empty((m,n))

#PRINT THE SIZE OF THE RATINGS MATRIX
print "Number of rows = %d " % RatingsMatrix.shape[0]
print "Number of cols = %d " % RatingsMatrix.shape[1]

#WALK THROUGH THE RATINGS MATRIX TO PUT -1 EVERYWHERE
#SO IN CASE OF AN EMPTY RATING, WE KNOW IT IS EMPTY
#BECAUSE OF THE NEGATIVE NUMBER

print "CREATING THE RATINGS MATRIX....."
for i in range(0,RatingsMatrix.shape[0]) :
        for j in range(0,RatingsMatrix.shape[1]):
                RatingsMatrix[i,j] = -1.



#NOW FILL THE RATINGS MATRIX
print "FILLING THE RATINGS MATRIX"
f = open(ratings_file , "r")
line_no = 0
lines = f.readlines()
test_set = random.sample(range(len(lines)),len(lines)/4)
test_ratings=[]
for line in lines:
        parts = line.split('::')
        user_name = parts[0]
        movie_name = parts[1]
        rating = parts[2]
		i = users_list.index(user_name)
		j = movies_list.index(movie_name)
		if line_no in test_set:
			test_ratings.append((float(rating),i,j))
		else:
			RatingsMatrix[i,j] = float(rating)
        line_no = line_no + 1
		
RatingsMatrix = RatingsMatrix[:(RatingsMatrix.shape[0]/2),:(RatingsMatrix.shape[1]/2)]

#PICKLE THE MATRIX FOR FUTURE USE
print "TIME TO PICKLE THE MATRIX"
outfile = open("train.dump" , "w")
pickle.dump(RatingsMatrix, outfile)
outfile.close()
outfile = open("test.dump" , "w")
pickle.dump(test_ratings, outfile)
outfile.close()

